﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using V5_DBUtility;
using System.Data;
using V5_Model;
using System.Data.SqlClient;

namespace V5_DAL.SqlServer {
    public class DALGZProductOrder {
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid ID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Bill_StockOutOrder");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier,16)};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(ModelGZProductOrder model) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Bill_StockOutOrder(");
            strSql.Append("ID,BillNo,StartBillNo,AgenciesID,SellerID,CustomID,DeliveryDate,DeliveryLocation,CreateUserNo,CreateDatetime,Effectdatetime,EffectUserID,Takedate,BillStatus,ComputerName,Status,Remark,PayStatus)");
            strSql.Append(" values (");
            strSql.Append("@ID,@BillNo,@StartBillNo,@AgenciesID,@SellerID,@CustomID,@DeliveryDate,@DeliveryLocation,@CreateUserNo,@CreateDatetime,@Effectdatetime,@EffectUserID,@Takedate,@BillStatus,@ComputerName,@Status,@Remark,@PayStatus)");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@BillNo", SqlDbType.VarChar,100),
					new SqlParameter("@StartBillNo", SqlDbType.VarChar,200),
					new SqlParameter("@AgenciesID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@SellerID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@CustomID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@DeliveryDate", SqlDbType.DateTime),
					new SqlParameter("@DeliveryLocation", SqlDbType.VarChar,200),
					new SqlParameter("@CreateUserNo", SqlDbType.VarChar,200),
					new SqlParameter("@CreateDatetime", SqlDbType.DateTime),
					new SqlParameter("@Effectdatetime", SqlDbType.DateTime),
					new SqlParameter("@EffectUserID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@Takedate", SqlDbType.DateTime),
					new SqlParameter("@BillStatus", SqlDbType.Bit,1),
					new SqlParameter("@ComputerName", SqlDbType.VarChar,200),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.VarChar,4000),
					new SqlParameter("@PayStatus", SqlDbType.Int,4) };
            parameters[0].Value = model.ID;
            parameters[1].Value = model.BillNo;
            parameters[2].Value = model.StartBillNo;
            parameters[3].Value = Guid.NewGuid();
            parameters[4].Value = Guid.NewGuid();
            parameters[5].Value = model.CustomID;
            parameters[6].Value = model.DeliveryDate;
            parameters[7].Value = model.DeliveryLocation;
            parameters[8].Value = model.CreateUserNo;
            parameters[9].Value = model.CreateDatetime;
            parameters[10].Value = model.Effectdatetime;
            parameters[11].Value = Guid.NewGuid();
            parameters[12].Value = model.Takedate;
            parameters[13].Value = model.BillStatus;
            parameters[14].Value = model.ComputerName;
            parameters[15].Value = model.Status;
            parameters[16].Value = model.Remark;
            parameters[17].Value = model.PayStatus;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ModelGZProductOrder model) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Bill_StockOutOrder set ");
            strSql.Append("BillNo=@BillNo,");
            strSql.Append("StartBillNo=@StartBillNo,");
            strSql.Append("AgenciesID=@AgenciesID,");
            strSql.Append("DeliveryDate=@DeliveryDate,");
            strSql.Append("DeliveryLocation=@DeliveryLocation,");
            strSql.Append("CreateUserNo=@CreateUserNo,");
            strSql.Append("CreateDatetime=@CreateDatetime,");
            strSql.Append("Effectdatetime=@Effectdatetime,");
            strSql.Append("EffectUserID=@EffectUserID,");
            strSql.Append("Takedate=@Takedate,");
            strSql.Append("BillStatus=@BillStatus,");
            strSql.Append("ComputerName=@ComputerName,");
            strSql.Append("Status=@Status,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("PayStatus=@PayStatus");
            strSql.Append(" where ID=@ID and SellerID=@SellerID and CustomID=@CustomID ");
            SqlParameter[] parameters = {
					new SqlParameter("@BillNo", SqlDbType.VarChar,100),
					new SqlParameter("@StartBillNo", SqlDbType.VarChar,200),
					new SqlParameter("@AgenciesID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@DeliveryDate", SqlDbType.DateTime),
					new SqlParameter("@DeliveryLocation", SqlDbType.VarChar,200),
					new SqlParameter("@CreateUserNo", SqlDbType.VarChar,200),
					new SqlParameter("@CreateDatetime", SqlDbType.DateTime),
					new SqlParameter("@Effectdatetime", SqlDbType.DateTime),
					new SqlParameter("@EffectUserID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@Takedate", SqlDbType.DateTime),
					new SqlParameter("@BillStatus", SqlDbType.Bit,1),
					new SqlParameter("@ComputerName", SqlDbType.VarChar,200),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.VarChar,4000),
					new SqlParameter("@PayStatus", SqlDbType.Int,4),
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@SellerID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@CustomID", SqlDbType.UniqueIdentifier,16)};
            parameters[0].Value = model.BillNo;
            parameters[1].Value = model.StartBillNo;
            parameters[2].Value = model.AgenciesID;
            parameters[3].Value = model.DeliveryDate;
            parameters[4].Value = model.DeliveryLocation;
            parameters[5].Value = model.CreateUserNo;
            parameters[6].Value = model.CreateDatetime;
            parameters[7].Value = model.Effectdatetime;
            parameters[8].Value = model.EffectUserID;
            parameters[9].Value = model.Takedate;
            parameters[10].Value = model.BillStatus;
            parameters[11].Value = model.ComputerName;
            parameters[12].Value = model.Status;
            parameters[13].Value = model.Remark;
            parameters[14].Value = model.PayStatus;
            parameters[15].Value = model.ID;
            parameters[16].Value = model.SellerID;
            parameters[17].Value = model.CustomID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(Guid ID) {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Bill_StockOutOrder ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier,16)};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelGZProductOrder GetModel(Guid ID) {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * From Bill_StockOutOrder ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier,16)};
            parameters[0].Value = ID;
            ModelGZProductOrder model = new ModelGZProductOrder();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0) {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "") {
                    model.ID = new Guid(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BillNo"] != null && ds.Tables[0].Rows[0]["BillNo"].ToString() != "") {
                    model.BillNo = ds.Tables[0].Rows[0]["BillNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["StartBillNo"] != null && ds.Tables[0].Rows[0]["StartBillNo"].ToString() != "") {
                    model.StartBillNo = ds.Tables[0].Rows[0]["StartBillNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["AgenciesID"] != null && ds.Tables[0].Rows[0]["AgenciesID"].ToString() != "") {
                    model.AgenciesID = new Guid(ds.Tables[0].Rows[0]["AgenciesID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SellerID"] != null && ds.Tables[0].Rows[0]["SellerID"].ToString() != "") {
                    model.SellerID = new Guid(ds.Tables[0].Rows[0]["SellerID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CustomID"] != null && ds.Tables[0].Rows[0]["CustomID"].ToString() != "") {
                    model.CustomID = new Guid(ds.Tables[0].Rows[0]["CustomID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DeliveryDate"] != null && ds.Tables[0].Rows[0]["DeliveryDate"].ToString() != "") {
                    model.DeliveryDate = DateTime.Parse(ds.Tables[0].Rows[0]["DeliveryDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DeliveryLocation"] != null && ds.Tables[0].Rows[0]["DeliveryLocation"].ToString() != "") {
                    model.DeliveryLocation = ds.Tables[0].Rows[0]["DeliveryLocation"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CreateUserNo"] != null && ds.Tables[0].Rows[0]["CreateUserNo"].ToString() != "") {
                    model.CreateUserNo = ds.Tables[0].Rows[0]["CreateUserNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CreateDatetime"] != null && ds.Tables[0].Rows[0]["CreateDatetime"].ToString() != "") {
                    model.CreateDatetime = DateTime.Parse(ds.Tables[0].Rows[0]["CreateDatetime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Effectdatetime"] != null && ds.Tables[0].Rows[0]["Effectdatetime"].ToString() != "") {
                    model.Effectdatetime = DateTime.Parse(ds.Tables[0].Rows[0]["Effectdatetime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EffectUserID"] != null && ds.Tables[0].Rows[0]["EffectUserID"].ToString() != "") {
                    model.EffectUserID = new Guid(ds.Tables[0].Rows[0]["EffectUserID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Takedate"] != null && ds.Tables[0].Rows[0]["Takedate"].ToString() != "") {
                    model.Takedate = DateTime.Parse(ds.Tables[0].Rows[0]["Takedate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BillStatus"] != null && ds.Tables[0].Rows[0]["BillStatus"].ToString() != "") {
                    if ((ds.Tables[0].Rows[0]["BillStatus"].ToString() == "1") || (ds.Tables[0].Rows[0]["BillStatus"].ToString().ToLower() == "true")) {
                        model.BillStatus = true;
                    }
                    else {
                        model.BillStatus = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["ComputerName"] != null && ds.Tables[0].Rows[0]["ComputerName"].ToString() != "") {
                    model.ComputerName = ds.Tables[0].Rows[0]["ComputerName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Status"] != null && ds.Tables[0].Rows[0]["Status"].ToString() != "") {
                    model.Status = int.Parse(ds.Tables[0].Rows[0]["Status"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "") {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }

                if (ds.Tables[0].Rows[0]["PayStatus"] != null && ds.Tables[0].Rows[0]["PayStatus"].ToString() != "")
                {
                    model.PayStatus = int.Parse(ds.Tables[0].Rows[0]["PayStatus"].ToString());
                }
                //if (ds.Tables[0].Rows[0]["TimeStamp"] != null && ds.Tables[0].Rows[0]["TimeStamp"].ToString() != "") {
                //    model.TimeStamp = DateTime.Parse(ds.Tables[0].Rows[0]["TimeStamp"].ToString());
                //}
                return model;
            }
            else {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM Bill_StockOutOrder ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0) {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM Bill_StockOutOrder ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Bill_StockOutOrder ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null) {
                return 0;
            }
            else {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim())) {
                strSql.Append("order by T." + orderby);
            }
            else {
                strSql.Append("order by T.CustomID desc");
            }
            strSql.Append(")AS Row, T.*  from Bill_StockOutOrder T ");
            if (!string.IsNullOrEmpty(strWhere.Trim())) {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Bill_StockOutOrder";
            parameters[1].Value = "CustomID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }

        public DataSet GetList(string strFiled, int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@OrderfldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@StatfldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
            parameters[0].Value = "Bill_StockOutOrder";
            parameters[1].Value = "*";
            parameters[2].Value = strOrder;
            parameters[3].Value = "CustomID";
            parameters[4].Value = PageSize;
            parameters[5].Value = PageIndex;
            parameters[6].Value = 1;
            parameters[7].Value = 1;
            parameters[8].Value = strWhere;
            DataSet ds = DbHelperSQL.RunProcedure("UP_GetRecordByPageOrder", parameters, "ds");
            RecordCount = int.Parse("0" + ds.Tables[1].Rows[0][0].ToString());
            return ds;
        }
        #endregion  Method
    }
}